package com.houdoudou.supplier.fragment;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.houdoudou.supplier.R;
import com.houdoudou.supplier.bean.Courier;
import com.houdoudou.supplier.bean.TransPasswordUtil;

import com.houdoudou.supplier.bean.Supplier;
import com.houdoudou.supplier.common.CommonFunction;
import com.houdoudou.supplier.common.Constant;
import com.houdoudou.supplier.common.UniversalHttpPost;

public class CourierBasicFrg  extends Fragment{

	
	String affair="";
    JSONObject courierJO= new JSONObject();
	EditText telnum,name,secret;
	TextView result,title;
	View.OnClickListener myOnclickListener;
	Button confirm;
	ProgressBar prgs;
    RelativeLayout rt_mother;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		 View view =inflater.inflate(R.layout.frg_courier_basic, container, false);
           init_listener();
           init_component(view);
           assign();
		 return view;
	}
	@Override
	public void setArguments(Bundle bundle) {
		// TODO Auto-generated method stub
		// super.setArguments(args);
		affair=bundle.getString("affair");
		if(affair.equals("updateCourier")){
			courierJO= JSONObject.fromObject(bundle.getString("courier"));
		}
	}
	
	private void assign(){
		if(affair.equals("createCourier")) {
			title.setText("新增快递员");
			telnum.setText("");
			name.setText("");
			secret.setText("");
		}else if(affair.equals("updateCourier")){			
			title.setText("修改分类");
			telnum.setText(courierJO.getString(Courier.TELNUM));
			name.setText(courierJO.getString(Courier.NAME));
			secret.setText("");
			telnum.setFocusable(false);
		}
	}
	private void init_listener(){
		
		
		myOnclickListener= new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				result.setText("");
				
				if( telnum.getText().toString().length()!=11){
					 new AlertDialog.Builder(getActivity()).setTitle("输入错误提示")
					 .setMessage("电话11位数").setPositiveButton("确定", null).show();
					return; 
					}else{
					courierJO.put(Courier.TELNUM, telnum.getText().toString());
					    }
				
				if( name.getText().toString().length()>20){
					 new AlertDialog.Builder(getActivity()).setTitle("输入错误提示")
					 .setMessage("名称不能超过20个字").setPositiveButton("确定", null).show();
					return;
					 
					}else{
					courierJO.put(Courier.NAME, name.getText().toString());
					    }
				String ciperText="";
				if( !CommonFunction.isNumeric(secret.getText().toString())||secret.getText().toString().isEmpty()){
					 new AlertDialog.Builder(getActivity()).setTitle("输入错误提示")
					 .setMessage("密码必须全为数字,且不能为空").setPositiveButton("确定", null).show();
					return;
					 
					}else{
						try { ciperText =TransPasswordUtil.encrypt(secret.getText().toString());} catch (Exception e) {e.printStackTrace(); }
				     	courierJO.put(Courier.SECRET, ciperText);
					    }
									  
			     courierJO.put(Courier.BELONG, Constant.supplierJO.getString(Supplier.TELNUM));
			     courierJO.put(Courier.TYPE, 3);
		              	
				 
				    result.setText("请稍后");
				    prgs.setVisibility(View.VISIBLE);
				    rt_mother.setFocusable(false);
					String[] params= new String[10];
	   	            params[0]=Constant.BaseUrl+Constant.SupplierUrl;
	                params[1]=affair; //PostCode
	                JSONObject jsonData= new JSONObject();
	                jsonData.put("supplier", Constant.supplierJO);
	                jsonData.put("courier", courierJO);
	                 params[2]=jsonData.toString();
	                new HttpPostTask().execute(params);
				  
		}};		
	}
		
	private void init_component(View view){
		
	
		telnum=(EditText) view.findViewById(R.id.et_telnum_frgcourierbasic);
		name=(EditText) view.findViewById(R.id.et_name_frgcourierbasic);
		secret=(EditText) view.findViewById(R.id.et_secret_frgcourierbasic);
	
		confirm=(Button) view.findViewById(R.id.btn_confirm_frgcourierbasic);
	
		result=(TextView) view.findViewById(R.id.tv_functionresult_frgcourierbasic);
		prgs= (ProgressBar) view.findViewById(R.id.prgs_frgcourierbasic);
		title=(TextView) view.findViewById(R.id.tv_mainttitle_frgcourierbasic);
		rt_mother = (RelativeLayout) view.findViewById(R.id.rt_mother_frgcourierbasic);
		//////////////////////
		
		confirm.setOnClickListener(myOnclickListener);
		prgs.setVisibility(View.INVISIBLE);
		
		
	}
	
	
	
	private  class HttpPostTask extends android.os.AsyncTask<String,String,Map<String,Object>>{
		@Override
		protected Map<String,Object> doInBackground(String... params) {
			// TODO Auto-generated method stub
			 Map<String,Object> map = new HashMap<String ,Object>();		
		  map =	UniversalHttpPost.doPost(params);	             
			return map;
		}
		
		@Override
		protected void onPostExecute(Map<String,Object> map) {
		
			 System.out.println("-----------onPostExecute()--------------");
			 prgs.setVisibility(View.GONE);
		     result.setText("");
		     rt_mother.setFocusable(true);
			 String PostResult=(String) map.get("PostResult");		 
			 boolean networkstate=(Boolean) map.get("PostState");
			 if(!networkstate){
				 CommonFunction.alert(getActivity(), "失败", "无网络连接");return;
		      
			}
			//	Toast.makeText(getActivity(), "服务器返回数据为"+PostResult, Toast.LENGTH_SHORT).show();
			    JSONObject json = null;
			    int resultCode=-1;
				String resultInfo="";
				String affair="";				
				JSONObject jo;
				try{
			     json= JSONObject.fromObject(PostResult);
			     resultCode=json.getInt("resultCode");
			     resultInfo=json.getString("resultInfo");	
			     affair =json.getString("affair");	
			     jo= json.getJSONObject("jo");
				}catch(JSONException e){
					 CommonFunction.alert(getActivity(), "失败", "数据解析错误");return;
				}	
		      
				 if(resultCode==1){ CommonFunction.alert(getActivity(), "失败", resultInfo);return; }
				 
				 if(resultCode==2){ CommonFunction.alert(getActivity(), "成功", resultInfo);return; }
		     
						
		} //onPostExecute
	} //AsyncTask
}
